package com.howells.time.module.personal.presenter;


import com.howells.time.base.http.HttpURL;
import com.howells.time.base.http.HttpUtils;
import com.howells.time.base.http.JsonCallback;
import com.howells.time.module.personal.view.UpdateInfoView;
import com.orhanobut.logger.Logger;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by Administrator on 2016.4.7.
 */
public class UpdateInfoPresenterImp implements UpdateInfoPresenter {
    private UpdateInfoView updateInfoView;

    public UpdateInfoPresenterImp(UpdateInfoView updateInfoView) {
        this.updateInfoView = updateInfoView;
    }

    @Override
    public void updateinfo(final Map<String,String> info) {
        HttpUtils.PostObject(HttpURL.ChangeInfoApi.toString(), info, new JsonCallback() {
            @Override
            public void onError(Call call, Exception e,int id) {
                Logger.d(e);
                updateInfoView.resultMsg("网络异常");
            }

            @Override
            public void onResponse(Map<String, Object> response,int id) {
                if(response.get("message").equals("success")){
                    updateInfoView.updatesuccess(info);
                }else {
                    Logger.d(response);
                    updateInfoView.resultMsg(response.get("message").toString());
                }
            }
        });

    }

    @Override
    public void upload(final String url) {
        Map<String,String> head=new HashMap<>();
        head.put("Content-Type","image/png");
        HttpUtils.PostFile(HttpURL.UploadApi.toString(), new File(url),head, new JsonCallback() {
            @Override
            public void onError(Call call, Exception e,int id) {
                Logger.d(e);
                updateInfoView.resultMsg("网络异常");
            }

            @Override
            public void onResponse(Map<String, Object> response,int id) {
                if(response.get("message").equals("success")) {
                    updateInfoView.resultMsg("修改头像成功");
                }
                else {
                    Logger.d(response);
                    updateInfoView.resultMsg(response.toString());
                }
            }
        });
    }
}
